﻿using System.Collections.Generic;
using RestSharp;

namespace Gx.Common
{
    public sealed class RestSharpHelper
    {
        #region Get
        /// <summary>
        /// 基本的请求函数
        /// </summary>
        /// <param name="url">请求路径</param>
        /// <param name="jsonBody">发送body</param>
        /// <returns></returns>
        public static string Get(string url, string jsonBody)
        {
            return RestSharp(url, Method.Get, null, jsonBody, null, null);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="url">请求路径</param>
        /// <param name="headList">头部参数</param>
        /// <param name="jsonBody">发送body</param>
        /// <returns></returns>
        public static string Get(string url, Dictionary<string, string> headList, string jsonBody)
        {
            return RestSharp(url, Method.Get, headList, jsonBody, null, null);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="url">请求路径</param>
        /// <param name="headList">头部参数</param>
        /// <param name="jsonBody">发送body</param>
        /// <param name="accept">Accept代表发送端（客户端）希望接受的数据类型(application/json)</param>
        /// <param name="content_type">代表发送端（客户端|服务器）发送的实体数据的数据类型(application/json;charset=utf-8)</param>
        /// <returns></returns>
        public static string Get(string url,
            Dictionary<string, string> headList,
            string jsonBody,
            string accept,
            string content_type)
        {
            return RestSharp(url, Method.Get, headList, jsonBody, accept, content_type);
        }


        /// <summary>
        /// 一般用于表单提交以及图片上传
        /// </summary>
        /// <param name="url">请求路径</param>
        /// <param name="bodyList">发送body</param>
        /// <returns></returns>
        public static string Get(string url, Dictionary<string, string> bodyList)
        {
            return RestSharp(url, Method.Get, null, bodyList, null, null);
        }

        /// <summary>
        /// 一般用于表单提交以及图片上传
        /// </summary>
        /// <param name="url">请求路径</param>
        /// <param name="headList">头部参数</param>
        /// <param name="bodyList">发送body</param>
        /// <returns></returns>
        public static string Get(string url, Dictionary<string, string> headList, Dictionary<string, string> bodyList)
        {
            return RestSharp(url, Method.Get, headList, bodyList, null, null);
        }


        /// <summary>
        /// 一般用于表单提交以及图片上传
        /// </summary>
        /// <param name="url">请求路径</param>
        /// <param name="headList">头部参数</param>
        /// <param name="bodyList">发送body</param>
        /// <param name="accept">Accept代表发送端（客户端）希望接受的数据类型</param>
        /// <param name="content_type">代表发送端（客户端|服务器）发送的实体数据的数据类型</param>
        /// <returns></returns>
        public static string Get(string url, string jsonBody, Dictionary<string, string> headList, Dictionary<string, string> bodyList,
            string accept,
            string content_type)
        {
            return RestSharp(url, Method.Get, headList, bodyList, accept, content_type);
        }
        #endregion

        #region Post

        /// <summary>
        /// 基本的请求函数
        /// </summary>
        /// <param name="url">请求路径</param>
        /// <param name="jsonBody">发送body</param>
        /// <returns></returns>
        public static string Post(string url, string jsonBody)
        {
            return RestSharp(url, Method.Post, null, jsonBody, null, null);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="url">请求路径</param>
        /// <param name="headList">头部参数</param>
        /// <param name="jsonBody">发送body</param>
        /// <returns></returns>
        public static string Post(string url, Dictionary<string, string> headList, string jsonBody)
        {
            return RestSharp(url, Method.Post, headList, jsonBody, null, null);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="url">请求路径</param>
        /// <param name="headList">头部参数</param>
        /// <param name="jsonBody">发送body</param>
        /// <param name="accept">Accept代表发送端（客户端）希望接受的数据类型(application/json)</param>
        /// <param name="content_type">代表发送端（客户端|服务器）发送的实体数据的数据类型(application/json;charset=utf-8)</param>
        /// <returns></returns>
        public static string Post(string url,
            Dictionary<string, string> headList,
            string jsonBody,
            string accept,
            string content_type)
        {
            return RestSharp(url, Method.Post, headList, jsonBody, accept, content_type);
        }


        /// <summary>
        /// 一般用于表单提交以及图片上传
        /// </summary>
        /// <param name="url">请求路径</param>
        /// <param name="bodyList">发送body</param>
        /// <returns></returns>
        public static string Post(string url, Dictionary<string, string> bodyList)
        {
            return RestSharp(url, Method.Post, null, bodyList, null, null);
        }

        /// <summary>
        /// 一般用于表单提交以及图片上传
        /// </summary>
        /// <param name="url">请求路径</param>
        /// <param name="headList">头部参数</param>
        /// <param name="bodyList">发送body</param>
        /// <returns></returns>
        public static string Post(string url, Dictionary<string, string> headList, Dictionary<string, string> bodyList)
        {
            return RestSharp(url, Method.Post, headList, bodyList, null, null);
        }


        /// <summary>
        /// 一般用于表单提交以及图片上传
        /// </summary>
        /// <param name="url">请求路径</param>
        /// <param name="headList">头部参数</param>
        /// <param name="bodyList">发送body</param>
        /// <param name="accept">Accept代表发送端（客户端）希望接受的数据类型</param>
        /// <param name="content_type">代表发送端（客户端|服务器）发送的实体数据的数据类型</param>
        /// <returns></returns>
        public static string Post(string url, string jsonBody, Dictionary<string, string> headList, Dictionary<string, string> bodyList,
            string accept,
            string content_type)
        {
            return RestSharp(url, Method.Post, headList, bodyList, accept, content_type);
        }

        #endregion

        #region BaseMethod

        /// <summary>
        /// 
        /// </summary>
        /// <param name="url">请求路径</param>
        /// <param name="method">请求类型</param>
        /// <param name="headList">头部参数</param>
        /// <param name="bodyList">发送body</param>
        /// <param name="accept">Accept代表发送端（客户端）希望接受的数据类型</param>
        /// <param name="content_type">代表发送端（客户端|服务器）发送的实体数据的数据类型</param>
        /// <returns></returns>
        private static string RestSharp(string url,
            Method method,
            Dictionary<string, string> headList,
            Dictionary<string, string> bodyList,
            string accept,
            string content_type)
        {
            var client = new RestClient(url);
            var request = new RestRequest
            {
                Method = method
            };
            // string content_type = "application/json;charset=utf-8";
            //  string accept = "application/json";
            if (!string.IsNullOrEmpty(accept))
            {
                request.AddHeader("Accept", accept);
            }
            if (!string.IsNullOrEmpty(content_type))
            {
                request.AddHeader("content-type", content_type);
            }
            foreach (var item in headList)
            {
                request.AddHeader(item.Key, item.Value);
            }
            foreach (var item in bodyList)
            {
                request.AddParameter(item.Key, item.Value);
            }
            var response = client.Execute(request);
            return response.Content;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url">请求路径</param>
        /// <param name="method">请求类型</param>
        /// <param name="jsonBody">发送body</param>
        /// <param name="headList">头部参数</param>
        /// <param name="Accept">Accept代表发送端（客户端）希望接受的数据类型。 </param>
        /// <param name="content_type">代表发送端（客户端|服务器）发送的实体数据的数据类型。 </param>
        /// <returns></returns>
        private static string RestSharp(string url,
            Method method,
            Dictionary<string, string> headList,
            string jsonBody,
            string accept,
            string content_type)
        {
            var client = new RestClient(url);
            var request = new RestRequest
            {
                Method = method
            };
            if (!string.IsNullOrEmpty(accept))
            {
                request.AddHeader("Accept", accept);
            }
            if (!string.IsNullOrEmpty(content_type))
            {
                request.AddHeader("content-type", content_type);
            }
            if (headList != null)
            {
                foreach (var item in headList)
                {
                    request.AddHeader(item.Key, item.Value);
                }
            }
            request.AddParameter("application/json", jsonBody, ParameterType.RequestBody);
            var response = client.Execute(request);
            return response.Content;
        }
        #endregion

    }
}